//
//  ViewController.m
//  UI19_JSON
//
//  Created by dllo on 16/8/9.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#define kCollectionView @"reuse"
#import "LabelCollectionViewCell.h"
#import "LabelCollectionViewLayout.h"
#import <AVFoundation/AVFoundation.h>
#import "SelfSizeTool.h"
#import "ContentTitle.h"

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout, LabelCollectionViewLayoutDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *labelTextArray;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    
  
    [self handleData];
    [self createCollectionView];
}

- (void)handleData {
    // JSON解析
    // 通过一个Data 将JSON格式转换成OC认识的内容(数组或字典)
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"FirstJSON" ofType:@".json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:dataPath];
    // JSON解析
    NSError *error = nil;
    NSMutableDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    // 一定要初始化
    self.labelTextArray = [NSMutableArray array];
    for (NSDictionary *dic in jsonDic[@"tabs"][@"list"]) {
        // 遍历构造器 赋值
        ContentTitle *contentTitle = [ContentTitle contentTitleWithDictionary:dic];
        [self.labelTextArray addObject:contentTitle];
    }
    NSLog(@"%@", jsonDic[@"tabs"][@"list"]);
    NSLog(@"%@", self.labelTextArray);

}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout height:(CGFloat)height widthForItemAtIndexPath:(NSIndexPath *)indexPath {
    return [SelfSizeTool selfSizeWidthWithString:[self.labelTextArray[indexPath.row] title]height:height font:[UIFont systemFontOfSize:17]];
}

- (void)createCollectionView {
    LabelCollectionViewLayout *layout = [LabelCollectionViewLayout new];
    layout.delegate = self;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor cyanColor];
    [self.collectionView registerNib:[UINib nibWithNibName:@"LabelCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCollectionView];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.labelTextArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LabelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionView forIndexPath:indexPath];
    cell.backgroundColor = [UIColor yellowColor];
    cell.label.text = [self.labelTextArray[indexPath.row] title];
    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
